//
//  ContactCell.m
//
//  Created by Olof Millberg on 2010-04-06.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <AddressBook/AddressBook.h>
#import "ContactCell.h"

@implementation ContactCell

@synthesize contactName;
@synthesize contactButton;
@synthesize deleteButton;
@synthesize contactPerson;
@synthesize contactImage;
@synthesize viewController;
@synthesize editable;
@synthesize delegate;

- (void) setContactPerson:(WOPerson *)aPerson
{
	[contactPerson release];
	contactPerson = [aPerson retain];
	self.contactName.text = [NSString stringWithFormat:@"%@ %@", contactPerson.firstName, contactPerson.lastName];
}

- (void) setEditable:(BOOL)isEditable
{
	editable = isEditable;
	if (!editable)
	{
		self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		self.deleteButton.hidden = YES;
	}
	else 
	{
		self.accessoryType = UITableViewCellAccessoryNone;
		self.deleteButton.hidden = NO;
	}	
}

- (IBAction) viewContact
{
	[self setSelected:NO animated:YES];
	//[self performSelector:@selector(deselect) withObject:nil afterDelay:0.1];
	
	//Initialize stuff.
	ABPersonViewController *personViewController = [[ABPersonViewController alloc] init];
	ABAddressBookRef myAddressBook = ABAddressBookCreate();
	ABRecordRef theRecord = [self getABRecordRefFromAB:myAddressBook];

	//Set stuff to display.
	personViewController.title = @"Info";
	personViewController.allowsEditing = YES;
	personViewController.addressBook = myAddressBook;
	personViewController.displayedPerson = theRecord;
	personViewController.personViewDelegate = self;
	personViewController.displayedProperties = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],
																		 [NSNumber numberWithInt:kABPersonEmailProperty], nil];
	[self.viewController.navigationController pushViewController:personViewController animated:YES];
    
	//Deallocate stuff.
	if (myAddressBook != NULL) 
	{
		CFRelease(myAddressBook);
	}
}

- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person 
					property:(ABPropertyID)property 
				  identifier:(ABMultiValueIdentifier)identifierForValue
{
	//Phone number tapped.
	if(property == kABPersonPhoneProperty)
	{
		//Get number and set up call launch.
		ABMultiValueRef phoneProperty = ABRecordCopyValue(person,property);
		NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phoneProperty,identifierForValue);
		NSString *phoneReadyToCall = [NSString stringWithFormat:@"tel://%@", phone];
		[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneReadyToCall]];
		
		//Release stuff.
		[phone release];
		if(phoneProperty != NULL)
			CFRelease(phoneProperty);
	}
	//Email address tapped.
	else if(property == kABPersonEmailProperty)
	{
		//Get address and set up mail lunch.
		ABMultiValueRef emailProperty = ABRecordCopyValue(person,property);
		NSString *email = (NSString *)ABMultiValueCopyValueAtIndex(emailProperty,identifierForValue);
		NSString *emailReadyToSend = [NSString stringWithFormat:@"mailto://%@", email];
		[[UIApplication sharedApplication] openURL:[NSURL URLWithString:emailReadyToSend]];
		
		//Release stuff.
		[email release];
		if(emailProperty != NULL)
			CFRelease(emailProperty);
	}
	[personViewController release];
	return YES;
}

- (ABRecordRef) getABRecordRefFromAB:(ABAddressBookRef)iPhoneAddressBook
{
	CFArrayRef allContacts = ABAddressBookCopyArrayOfAllPeople(iPhoneAddressBook);
	CFIndex numberOfContacts = ABAddressBookGetPersonCount(iPhoneAddressBook);
	ABRecordRef currentContact;	
	
	for (int i = 0; i < numberOfContacts; i++) 
	{
		currentContact = CFArrayGetValueAtIndex(allContacts, i);
		NSString *theSocialID = (NSString *)ABRecordCopyValue(currentContact, kABPersonNoteProperty);
		
		if([theSocialID isEqualToString: self.contactPerson.socialID])
		{
			[theSocialID release];
			break;
		}
		else
		{
			[theSocialID release];
		}
	}
	
	//Release stuff.
	if( allContacts != NULL)
		CFRelease(allContacts);
	
	return currentContact;	
}

- (IBAction) deleteContact
{
	[delegate contactCellDeleteButtonPressed:self];
}

- (void) select
{
	[self setSelected:YES animated:NO];
}

- (void) deselect
{
	[self setSelected:NO animated:YES];
}

@end
